package org.dftproject.genesis.ui.pages.summary.sources;

import java.util.HashMap;
import java.util.Map;

import org.dftproject.genesis.data.IAttribute;
import org.dftproject.genesis.data.genealogy.ISource;
import org.dftproject.genesis.ui.GenesisResources;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.ToolbarLayout;
import org.eclipse.draw2d.Viewport;
import org.eclipse.jface.resource.JFaceResources;

public class FootnoteContainer extends Figure {
	
	private final Viewport viewport;
	
	private final Map<ISource, Footnote> footnoteBySource = new HashMap<ISource, Footnote>();
	
	public FootnoteContainer(Viewport viewport) {
		this.viewport = viewport;
		
		ToolbarLayout layout = new ToolbarLayout();
		layout.setSpacing(10);
		layout.setStretchMinorAxis(false);
		setLayoutManager(layout);
		
		Label label = new Label("Sources");
		label.setFont(JFaceResources.getFont(GenesisResources.H1_FONT));
		add(label);
	}
	
	public void addSource(IAttribute sourceAttribute, FootnoteMarker marker) {
		Object source = sourceAttribute.getValue();
		if (source instanceof ISource) {
			Footnote footnote = footnoteBySource.get(source);
			if (footnote == null) {
				footnote = new Footnote(getChildren().size(), (ISource) source);
				add(footnote);
				footnoteBySource.put((ISource) source, footnote);
			}
			
			footnote.addAttribute(sourceAttribute);
			marker.setFootnote((ISource) source, footnote);
		}
	}

	public void scrollTo(Footnote footnote) {
		viewport.setViewLocation(footnote.getLocation());
	}

}
